


Page({
    data:{
        groupCode:'',
        statusImgUrl:'',
        statusTitle:'',
        commanderUrl:'',
        memberUrls:[],
        defaultCommanderUrl:"../../image/xcx_share.png",
        statusColor:'',
        statusBtn:'' 
    },
    onLoad:function(options){
        //let groupCode=options.groupCode;
        this.setData({
            groupCode:options.groupCode
        });
        this.getUserId();
    },
    getUserId: function() {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        var _self = this;
        wx.login({
            success: function(res) {
                wx.hideLoading();
                if (res.code) {
                    var code = res.code;
                    console.log(code);
                    // return;
                    _self.acquireOpenId(code)
                } else {

                }
            },
            fail: function(res) {
                console.log(res)
            }
        });

    },
    // //用code获取openId,是否是新用户,并缓存 
    acquireOpenId: function(code) {
        var _self = this;
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/autoLogin.do',
            data: {
                code: code
            },
            success: function(response) {
                wx.hideLoading();
                const res = response.data
                if (res.code == 200) {
                    if (res.obj.first) {
                        _self.setData({
                            isFirst: true,
                            openid: res.obj.openId
                        });
                        wx.setStorageSync('openId', res.obj.openId);
                        wx.setStorageSync('memberId', res.obj.memId);
                        wx.setStorageSync('newUser', true);
                        console.log(11);
                       // _self.getUserInfo();
                    } else {
                        console.log("老用户");
                        //老用户   
                        _self.setData({
                            isFirst: false,
                            openid: res.obj.openId
                        });
                        wx.setStorageSync('memberId', res.obj.memId);
                        wx.setStorageSync('openId', res.obj.openId);
                        _self.appletLogin();
                    }
                    return
                } else {
                    _self.setData({
                        isFirst: false
                    });
                    _self.showMyToast(_self, res.des)
                }
            }
        })
    },
    // //老用户根据openid登录
    appletLogin: function() {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        var _self = this
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/login.do',
            method: 'get',
            data: {
                openId: wx.getStorageSync('openId'),
            },
            success: function(res) {
                wx.hideLoading();
                if (res.data.code == 200) {
                    getApp().globalData.userInfo = res.data.obj;
                    getApp().globalData.header.Cookie = 'SESSION=' + res.data.obj.session;
                    _self.getGroupOrder();
                } else {
                    _self.setData({
                        isFirst: false
                    });
                }
            }
        })
    },
    //获取订单拼团信息
    getGroupOrder(){
        let that=this;
        wx.request({
			url: getApp().globalData.baseUrl + '/applet/group/getGroupDetail.do',
			data: {
                groupCode:that.data.groupCode , 	
			},
            header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				if (res.data.code == 200) {
                   let status='';
                   let statusTitle='';
                   let statusColor='';
                   let statusBtn='';
                    switch (res.data.obj.status){
                         case 1://失败
                         status="../../image/fail.png";
                         statusTitle="拼团失败";
                         statusColor="hit-red";
                         statusBtn="我要做团长";
                         break;
                         case 2://成功
                         status="../../image/success.png";
                         statusTitle="拼团成功";
                         statusColor="hit-green";
                         statusBtn="查看订单";
                         break;
                    }
                    that.setData({
                        statusImgUrl:status,
                        statusTitle:statusTitle,
                        statusColor:statusColor,
                        statusBtn:statusBtn,
                        commanderUrl:res.data.obj.commanderUrl,
                        memberUrls:res.data.obj.memberUrls
                    });
				
    
				} else {

				}
			}
		})
    }
   
});